<?php defined('SYSPATH') or die('No direct script access.');

class Exception_Handler_Dec {
	
    public static function handle(Exception $e)
    {
		switch (get_class($e))
        {
            case 'Http_Exception_404':
                $response = new Response;
                $response->status(404);
				$view = Smarty_View::factory('error/404.tpl');
                break;
            default:
				$response = new Response;
				$view = Smarty_View::factory('error/handler.tpl');
                break;
        }
        $log_error_type = (isset(Kohana::$config->load("dec")->log_error_type) ? Kohana::$config->load("dec")->log_error_type : "display");
        
        if("email" == $log_error_type){
            if(isset(Kohana::$config->load("dec")->log_to_email) && isset(Kohana::$config->load("dec")->sys_name)){
                @mail(Kohana::$config->load("dec")->log_to_email, "Erro ".Kohana::$config->load("dec")->sys_name, $e."<br />\n".str_replace("\n", "<br />\n", $e->getTraceAsString()));
            }else{
                $log_error_type = "display";
            }
        }
        if("log" == $log_error_type){
            Kohana::$log->add(Log::ERROR, Kohana_Exception::text($e));
        }
        if("display" == $log_error_type){
            Kohana_Exception::handler($e);
        }
        echo $response->body($view)->send_headers()->body();
        return TRUE;
	}
}